package com.polaris.live.ui.party.widget.seat.party_seat

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.blankj.utilcode.util.CollectionUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.WidgetAudioPartySeatBinding
import com.polaris.live.im.sfs.bean.live.LiveRoomInfo
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.resp.back_resp.UserRoomThemeResp
import com.polaris.live.ui.party.widget.seat.PartyStreamProxy
import com.polaris.live.ui.party.widget.seat.base.BaseSeatItemView
import com.polaris.live.ui.party.widget.seat.base.BaseSeatView
import com.polaris.live.ui.party.widget.seat.bean.SeatBean
import com.polaris.live.ui.party.widget.seat.listener.PartySeatListener
import com.polaris.live.ui.party.widget.seat.party_seat.view.AmusementSeatView
import com.polaris.live.ui.party.widget.seat.party_seat.view.CpSeatView
import com.polaris.live.ui.party.widget.seat.party_seat.view.EmperorSeatView
import com.polaris.live.ui.party.widget.seat.party_seat.view.HonorSeatView
import com.polaris.live.ui.party.widget.seat.party_seat.view.PartySeatView
import com.polaris.live.ui.party.widget.seat.party_seat.view.SupremeSeatView

/**
 * 语言房所有座位类型控件
 * @author Created by 天晴 on 2024/2/26 9:12
 * @since 1.0.0
 **/
class PartyAudioSeatView(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {

    val mBinding = WidgetAudioPartySeatBinding.inflate(LayoutInflater.from(context), this)
    var listener: PartySeatListener? = null

    /**
     * 主题类型
     */
    var theme: Int = -1

    private var seatView: BaseSeatView? = null

    init {
        clipChildren = false
        clipToPadding = false
    }

    /**
     * 设置加入时的麦位数据全部,初始化  theme主题  默认为普通样式8个位置
     */
    fun setInitJoinData(mLiveInfo: LiveRoomInfo, userId: Long, lastPartyStreamProxy: PartyStreamProxy? = null) {
        this.theme = mLiveInfo.theme ?: 0
        when (theme) {
            LiveConst.THEME_TYPE.THEME_DEFAULT -> {
                seatView = PartySeatView(context, null)
            }

            LiveConst.THEME_TYPE.LIVE_PARTY_THEME_COUPLE -> {
                seatView = CpSeatView(context, null)
                val lp = layoutParams as MarginLayoutParams
                lp.topMargin = 0
                layoutParams = lp
            }

            LiveConst.THEME_TYPE.LIVE_PARTY_THEME_EMPEROR -> {
                seatView = EmperorSeatView(context, null)
            }

            LiveConst.THEME_TYPE.LIVE_PARTY_THEME_HONOR -> {
                seatView = HonorSeatView(context, null)
            }

            LiveConst.THEME_TYPE.LIVE_PARTY_THEME_SUPREME -> {
                seatView = SupremeSeatView(context, null)
            }

            LiveConst.THEME_TYPE.LIVE_PARTY_THEME_AMUSEMENT -> {
                seatView = AmusementSeatView(context, null)
            }
        }

        listener?.let {
            setPartySeatListener(it);
        }

        seatView?.apply {
            layoutParams =
                LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        (mBinding.root as ViewGroup).removeAllViews()
        (mBinding.root as ViewGroup).addView(seatView)
        seatView?.setInitJoinData(mLiveInfo, userId, lastPartyStreamProxy)

    }

    /**
     * 更新单个座位数据
     */
    fun setUpdateDateSeat(seatId: Int, mLiveInfo: LiveRoomInfo) {
        seatView?.setUpdateDateSeat(seatId, mLiveInfo)
    }

    fun setPartySeatListener(listener: PartySeatListener) {
        if (theme != -1) {
            seatView?.setPartySeatListener(listener)
        } else {
            this.listener = listener
        }

    }

    fun getSeatViewAddr(): List<Pair<String, View>> {
        if (seatView == null) {
            return listOf()
        }
        return when (theme) {
            LiveConst.THEME_TYPE.THEME_DEFAULT -> {
                val partySeat = seatView as PartySeatView
                listOf(
                    Pair("1", partySeat.mBinding.seat1),
                    Pair("2", partySeat.mBinding.seat2),
                    Pair("3", partySeat.mBinding.seat3),
                    Pair("4", partySeat.mBinding.seat4),
                    Pair("5", partySeat.mBinding.seat5),
                    Pair("6", partySeat.mBinding.seat6),
                    Pair("7", partySeat.mBinding.seat7),
                    Pair("8", partySeat.mBinding.seat8)
                )
            }

            LiveConst.THEME_TYPE.LIVE_PARTY_THEME_COUPLE -> {
                val partyCpSeat = seatView as CpSeatView
                listOf(
                    Pair("1", partyCpSeat.mBinding.seat1),
                    Pair("2", partyCpSeat.mBinding.seat2),
                    Pair("3", partyCpSeat.mBinding.seat3),
                    Pair("4", partyCpSeat.mBinding.seat4),
                    Pair("5", partyCpSeat.mBinding.seat5),
                    Pair("6", partyCpSeat.mBinding.seat6),
                    Pair("7", partyCpSeat.mBinding.seat7),
                    Pair("8", partyCpSeat.mBinding.seat8),
                    Pair("9", partyCpSeat.mBinding.seat9),
                    Pair("10", partyCpSeat.mBinding.seat10),
                    Pair("11", partyCpSeat.mBinding.seat11),
                    Pair("12", partyCpSeat.mBinding.seat12),
                )
            }

            LiveConst.THEME_TYPE.LIVE_PARTY_THEME_EMPEROR -> {
                val partyEmperorSeat = seatView as EmperorSeatView
                listOf(
                    Pair("1", partyEmperorSeat.mBinding.seat1),
                    Pair("2", partyEmperorSeat.mBinding.seat2),
                    Pair("3", partyEmperorSeat.mBinding.seat3),
                    Pair("4", partyEmperorSeat.mBinding.seat4),
                    Pair("5", partyEmperorSeat.mBinding.seat5),
                    Pair("6", partyEmperorSeat.mBinding.seat6),
                    Pair("7", partyEmperorSeat.mBinding.seat7),
                    Pair("8", partyEmperorSeat.mBinding.seat8),
                    Pair("9", partyEmperorSeat.mBinding.seat9),
                    Pair("10", partyEmperorSeat.mBinding.seat10),
                    Pair("11", partyEmperorSeat.mBinding.seat11),
                )
            }

            LiveConst.THEME_TYPE.LIVE_PARTY_THEME_HONOR -> {
                val honorSeatView = seatView as HonorSeatView
                listOf(
                    Pair("1", honorSeatView.mBinding.seat1),
                    Pair("2", honorSeatView.mBinding.seat2),
                    Pair("3", honorSeatView.mBinding.seat3),
                    Pair("4", honorSeatView.mBinding.seat4),
                    Pair("5", honorSeatView.mBinding.seat5),
                    Pair("6", honorSeatView.mBinding.seat6),
                    Pair("7", honorSeatView.mBinding.seat7),
                    Pair("8", honorSeatView.mBinding.seat8),
                    Pair("9", honorSeatView.mBinding.seat9),
                    Pair("10", honorSeatView.mBinding.seat10),
                    Pair("11", honorSeatView.mBinding.seat11),
                    Pair("12", honorSeatView.mBinding.seat12),
                )
            }

            LiveConst.THEME_TYPE.LIVE_PARTY_THEME_SUPREME -> {
                val supremeSeatView = seatView as SupremeSeatView
                listOf(
                    Pair("1", supremeSeatView.mBinding.seat1),
                    Pair("2", supremeSeatView.mBinding.seat2),
                    Pair("3", supremeSeatView.mBinding.seat3),
                    Pair("4", supremeSeatView.mBinding.seat4),
                    Pair("5", supremeSeatView.mBinding.seat5),
                    Pair("6", supremeSeatView.mBinding.seat6),
                    Pair("7", supremeSeatView.mBinding.seat7),
                    Pair("8", supremeSeatView.mBinding.seat8),
                    Pair("9", supremeSeatView.mBinding.seat9),
                    Pair("10", supremeSeatView.mBinding.seat10),
                    Pair("11", supremeSeatView.mBinding.seat11),
                    Pair("12", supremeSeatView.mBinding.seat12),
                    Pair("13", supremeSeatView.mBinding.seat13),
                )
            }

            LiveConst.THEME_TYPE.LIVE_PARTY_THEME_AMUSEMENT -> {
                val amusementSeatView = seatView as AmusementSeatView

                listOf(
                    Pair("1", amusementSeatView.mBinding.seat1),
                    Pair("2", amusementSeatView.mBinding.seat2),
                    Pair("3", amusementSeatView.mBinding.seat3),
                    Pair("4", amusementSeatView.mBinding.seat4),
                    Pair("5", amusementSeatView.mBinding.seat5),
                    Pair("6", amusementSeatView.mBinding.seat6),
                    Pair("7", amusementSeatView.mBinding.seat7),
                    Pair("8", amusementSeatView.mBinding.seat8),
                    Pair("9", amusementSeatView.mBinding.seat9),
                    Pair("10", amusementSeatView.mBinding.seat10),
                    Pair("11", amusementSeatView.mBinding.seat11),
                    Pair("12", amusementSeatView.mBinding.seat12),
                    Pair("13", amusementSeatView.mBinding.seat13),
                    Pair("14", amusementSeatView.mBinding.seat14),
                    Pair("15", amusementSeatView.mBinding.seat15),
                )
            }

            else -> {
                listOf()
            }
        }

    }

    /**
     * 获取所有座位的用户信息
     */
    fun getSeatData(): MutableList<SeatBean> {
        return seatView?.getSeatData() ?: mutableListOf()
    }

    /**
     * 展示emoji
     */
    fun showUserEmoji(message: SendLiveMessage) {
        seatView?.showUserEmoji(message)
    }

    /**
     * 修改派对房用户职位
     */
    fun upDataUserPosition(data: List<Long>, userId: Long) {
        seatView?.upDataUserPosition(data, userId)
    }

    /**
     * 根据userId查找座位上是否有这个人
     */
    fun getSeatBeanByUserId(userId: Long): SeatBean? {
        return seatView?.getSeatBeanByUserId(userId)

    }

    /**
     * 获取自己座位信息
     */
    fun getMeSeatBean(): SeatBean? {
        return seatView?.getMeSeatBean()
    }

    /**
     * 获取一个无人的麦位
     */
    fun getNoHavaSeat(): SeatBean? {
        return seatView?.getNoHavaSeat()
    }

    /**
     * 检查麦上用户 推拉流
     */
    fun inspectMaishangZego() {
        seatView?.inspectMaishangZego()
    }

    /**
     * 设置皮肤
     */
    fun setTheme(userRoomTheme: UserRoomThemeResp? = null) {
        val viewList = getSeatViewAddr()
        if (CollectionUtils.isNotEmpty(viewList)) {
            viewList.forEach {
                val view = it.second
                if (view is BaseSeatItemView) {
                    view.upDateSkinView(userRoomTheme)
                }
            }
        }
    }

    /**
     * 获取流控制器
     */
    fun getPartyStreamProxy(): PartyStreamProxy? {
        return seatView?.getPartyStreamProxy()
    }

    /**
     * 重置
     */
    fun reset() {
        seatView?.reset()
    }

    fun onResume() {
        seatView?.onResume()
    }

    fun onPause() {
        seatView?.onPause()
    }

    /**
     * 释放
     */
    fun destroyView() {
        seatView?.destroyView()
    }

    fun setMinimize(minimize: Boolean) {
        seatView?.setMinimize(minimize)
    }
}